package com.gz.mylife.app;

import java.lang.ref.WeakReference;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.util.PrefUtils;
import com.gz.mylife.widget.FooterView;
import com.gz.mylife.widget.LoadingCover;
import com.gz.mylife.widget.FooterView.OnFooterViewRefreshListener;
import com.gz.mylife.widget.LoadingCover.OnLoadingCoverRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zl.smartmall.library.IGZAddress;
import com.zl.smartmall.library.listener.GetGroupByCmidAddressListener;
import com.zl.smartmall.library.listener.GetGroupsAddressListener;
import com.zl.smartmall.library.listener.RequestCode;
import com.zl.smartmall.library.po.GroupsInfo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SelectGroupsActivity extends BaseActivity implements
		OnItemClickListener, OnClickListener, OnLoadingCoverRefreshListener,
		OnRefreshListener<ListView>, OnFooterViewRefreshListener {
	private PullToRefreshListView mListView;
	private LoadingCover mloadCover;
	private List<GroupsInfo> groupsInfos;
	private int currentAddress = -1;
	private MenuItem ensureItem;
	private TextView titile;
	private String district_id;
	private int page;
	private FooterView mFooterView;
	private LayoutInflater mInflater;
	private MyAdapter mAdapter;
	private int cmid;
	//private GroupsListener groupsListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selectcommunity);
		titile = (TextView) findViewById(R.id.titile);
		titile.setText("请选择您所在的小区");
		district_id = getIntent().getStringExtra("district_id");
		cmid = getIntent().getIntExtra("cmid", 0);
		groupsInfos = new ArrayList<GroupsInfo>();
		setActionbarTitle("选择小区", true);
		initView();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.updata_menu, menu);
		ensureItem = menu.findItem(R.id.action_updata);
		MenuItemCompat.setShowAsAction(ensureItem,
				MenuItemCompat.SHOW_AS_ACTION_ALWAYS
						| MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		if (currentAddress == -1) {
			setActionbarTitle("选择小区", false);
		}
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_updata:
			if (currentAddress == -1) {
				Toast.makeText(this, "请先选择小区", Toast.LENGTH_SHORT).show();
				return true;
			}
			Intent intent = new Intent();
			intent.putExtra("obj", groupsInfos.get(currentAddress));
			String groupname = groupsInfos.get(currentAddress).getName();
			intent.putExtra("groupname", groupname);
			setResult(107, intent);
			finish();
			break;

		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	private void initView() {
		// groupAdapter = bulidAdapter();
		mAdapter = new MyAdapter();
		mInflater = getLayoutInflater();
		//groupsListener = new GroupsListener(this);
		mListView = (PullToRefreshListView) findViewById(R.id.lv_community);
		mListView.setAdapter(mAdapter);
		mListView.setMode(Mode.DISABLED);
		mListView.setOnItemClickListener(this);
		mListView.setOnRefreshListener(this);

		mFooterView = (FooterView) mInflater
				.inflate(R.layout.footer_view, null);
		mFooterView.setOnFooterViewRefreshListener(this);
		mListView.getRefreshableView().addFooterView(mFooterView);
		mListView.setOnScrollListener(mFooterView);
		mloadCover = (LoadingCover) findViewById(R.id.layout_loading_cover);
		mloadCover.setOnLoadingCoverRefreshListener(this);
		mloadCover.onRefreshSuccess();
		/*if ("".equals(district_id)||district_id==null) {
			mFooterView.setVisibility(View.GONE);
			loadgroupDatas();
		}else {
			loadgroupData(buildListener(RequestCode.PULL));
		}*/
		initdata();
		
	}

/*	// 获取小区的信息
	private void loadgroupData(GetGroupsAddressListener groupListener) {

		if (groupListener.getRequestCode() == RequestCode.PULL) {
			page = 1;
		} else if (groupListener.getRequestCode() == RequestCode.SCROLL) {
			page++;
		}
		IGZAddress.getInstance().getGroups(district_id, page, groupListener);
	}
	private void loadgroupDatas() {

		IGZAddress.getInstance().getGroupBycmid(cmid, groupsListener);
	}
*/
	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return groupsInfos.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return groupsInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.selectcommunity_item,null);
				holder = new ViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.selectimg = (ImageView) convertView.findViewById(R.id.iv_select);
				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}

			final GroupsInfo info = groupsInfos.get(position);
			holder.name.setText(info.getName());
			if (info.getStatus() == 0) {
				holder.selectimg.setVisibility(View.GONE);
			} else {
				holder.selectimg.setVisibility(View.VISIBLE);
			}
			return convertView;
		}

	}

	private class ViewHolder {
		TextView name;
		ImageView selectimg;

	}

	@Override
	public void onClick(View v) {

	}

	@Override
	public void onLoadingCoverRefresh() {
		//loadgroupData(buildListener(RequestCode.PULL));
	}

	private GetGroupsAddressListener buildListener(RequestCode requestCode) {
		return new GetGroupsAddressListener(requestCode) {
			@Override
			public void onResponseSuccess(List<GroupsInfo> lists) {

				if (this.requestCode == RequestCode.PULL) {
					groupsInfos.clear();
				}
				groupsInfos.addAll(lists);
				mAdapter.notifyDataSetChanged();
				if (lists.size() < Define.LISTVIEW_ITEM_MIN_SIZE) {
					mFooterView.onRefreshComplete();
					mListView.setOnScrollListener(null);
				} else {
					mAdapter.notifyDataSetChanged();
					mFooterView.onRefreshSuccess();// 刷新成功并且后面还有数据可以进行加载
					mListView.setOnScrollListener(mFooterView);
				}
				if (mListView.isRefreshing()) {
					mListView.getLoadingLayoutProxy().refreshSuccess();
				}
				mloadCover.onRefreshSuccess();
			}

			@Override
			public void onNoMoreData() {
				if (mListView.isRefreshing()) {
					mListView.getLoadingLayoutProxy().refreshSuccess();
				}
				mFooterView.onRefreshComplete();
				mloadCover.onRefreshFailed(Define.MSG_NO_MORE_DATA);
			}

			@Override
			public void onNoNetWork() {
				if (mListView.isRefreshing()) {
					mListView.getLoadingLayoutProxy().refreshFailed();
				}
				mFooterView.onRefreshFailed();
				mloadCover.onRefreshFailed(Define.MSG_NO_NETWORK);
			}

			@Override
			public void onResponseFailed() {
				mloadCover.onRefreshFailed();
				// mFooterView.onRefreshFailed();
				if (mListView.isRefreshing()) {
					mListView.getLoadingLayoutProxy().refreshFailed();

				}
			}

		};

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (view == mFooterView) {
			return;
		}
		if (id >= 0) {
			if (id != currentAddress) {
				if (currentAddress != -1) {
					groupsInfos.get(currentAddress).setStatus(0);
				} else {
					ensureItem.setVisible(true);
				}
				currentAddress = (int) id;
				groupsInfos.get(currentAddress).setStatus(1);
			}
			mAdapter.notifyDataSetChanged();
			//mAdapter.addAll(groupsInfos);
		}
	}

	@Override
	public void onFooterViewRefresh() {
		// TODO Auto-generated method stub
		//loadgroupData(buildListener(RequestCode.SCROLL));
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		//loadgroupData(buildListener(RequestCode.PULL));
	}
	
	/*private static class GroupsListener extends GetGroupByCmidAddressListener {
		private SelectGroupsActivity mOuter;

		public GroupsListener(SelectGroupsActivity outer) {
			this.mOuter = outer;
		}

		public void release() {
			new WeakReference<SelectGroupsActivity>(mOuter);
		}
		
		@Override
		public void onResponseSuccess(List<GroupsInfo> lists) {
			if(mOuter == null){
				return;
			}
			mOuter.groupsInfos.clear();
			mOuter.groupsInfos.addAll(lists);
			mOuter.mAdapter.notifyDataSetChanged();
			//mOuter.mAdapter.addAll(mOuter.groupsInfos);
			mOuter.mloadCover.onRefreshSuccess();
		}

		@Override
		public void onNoMoreData() {
			mOuter.mloadCover.onRefreshFailed("暂无小区数据");
		}

		@Override
		public void onNoNetWork() {
			mOuter.mloadCover.onRefreshFailed();
			Toast.makeText(mOuter, Define.MSG_NO_NETWORK, Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onResponseFailed() {
			mOuter.mloadCover.onRefreshFailed();
		}

	}*/
	
	private void initdata(){
		//初始化小区数据
				GroupsInfo groups1 = new GroupsInfo();
				groups1.setCmid(75);
				groups1.setCreatetime(new java.util.Date().getTime()+"");
				groups1.setId(309);
				groups1.setName("山水庭院");
				groups1.setStatus(0);
				groups1.setStorehouse("");
				groups1.setUstatus(1);
				groupsInfos.add(groups1);
				
				GroupsInfo groups2 = new GroupsInfo();
				groups2.setCmid(75);
				groups2.setCreatetime(new java.util.Date().getTime()+"");
				groups2.setId(310);
				groups2.setName("逸景公寓");
				groups2.setStatus(0);
				groups2.setStorehouse("");
				groups2.setUstatus(1);
				groupsInfos.add(groups2);
				
				GroupsInfo groups3 = new GroupsInfo();
				groups3.setCmid(75);
				groups3.setCreatetime(new java.util.Date().getTime()+"");
				groups3.setId(311);
				groups3.setName("滨河果岭");
				groups3.setStatus(0);
				groups3.setStorehouse("");
				groups3.setUstatus(1);
				groupsInfos.add(groups3);
				
				GroupsInfo groups4 = new GroupsInfo();
				groups4.setCmid(75);
				groups4.setCreatetime(new java.util.Date().getTime()+"");
				groups4.setId(312);
				groups4.setName("金融花园");
				groups4.setStatus(0);
				groups4.setStorehouse("");
				groups4.setUstatus(1);
				groupsInfos.add(groups4);
				
				GroupsInfo groups5 = new GroupsInfo();
				groups5.setCmid(75);
				groups5.setCreatetime(new java.util.Date().getTime()+"");
				groups5.setId(313);
				groups5.setName("桃园新居");
				groups5.setStatus(0);
				groups5.setStorehouse("");
				groups5.setUstatus(1);
				groupsInfos.add(groups5);
				
				GroupsInfo groups6 = new GroupsInfo();
				groups6.setCmid(75);
				groups6.setCreatetime(new java.util.Date().getTime()+"");
				groups6.setId(314);
				groups6.setName("五龙湾阳光海岸海滨区");
				groups6.setStatus(0);
				groups6.setStorehouse("");
				groups6.setUstatus(1);
				groupsInfos.add(groups6);
				
				GroupsInfo groups7 = new GroupsInfo();
				groups7.setCmid(75);
				groups7.setCreatetime(new java.util.Date().getTime()+"");
				groups7.setId(315);
				groups7.setName("五龙湾阳光海岸阳光区");
				groups7.setStatus(0);
				groups7.setStorehouse("");
				groups7.setUstatus(1);
				groupsInfos.add(groups7);
	}
}
